package com.servlet;

import com.dao.Flight;
import com.dao.FlightDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * Created by Phillip on 2014/4/29.
 */
public class Servlet extends javax.servlet.http.HttpServlet {

    FlightDaoImpl flightDao = new FlightDaoImpl();
    List<String> citylist = null;


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html");
//        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
//        List<String> citylist = (List<String>) session.getAttribute("citylist");
        System.out.println(session.getAttribute("username"));

        String deptcity = request.getParameter("deptcity");
        String destcity = request.getParameter("destcity");
        String deptdate = request.getParameter("deptdate");
        String returndate = request.getParameter("returndate");

        List<Flight> flights1 = flightDao.queryByCityDate(deptcity, destcity, "2014-03-" + deptdate, 3);
        List<Flight> flights2 = flightDao.queryByCityDate(destcity, deptcity, "2014-03-" + returndate, 3);
        flights1.addAll(flights2);

        System.out.println(flights1.size());

        if (flights1 == null)
            request.removeAttribute("flightlist");
        else
            request.setAttribute("flightlist", flights1);
//        request.setAttribute("deptdate", deptdate);
//        request.setAttribute("returndate", returndate);

        request.getRequestDispatcher("query.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//        FlightDaoImpl flightDao = new FlightDaoImpl();
        citylist = flightDao.queryCityList(3);
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();

        if (session.getAttribute("isLogon") == null || !(boolean) session.getAttribute("isLogon")) {
            out.println("<script type=\"text/javascript\" language=\"javascript\">");
            out.println("alert(\"Please login first\");");
            out.println("window.document.location.href=\"index.jsp\";</script>");
        }
        session.setAttribute("citylist", citylist);
        System.out.println(session.getAttribute("username"));

        response.setContentType("text/html");

        String title = "Flight Booking Centre";
//        out.println("<%@ page contentType=\"text/html;charset=UTF-8\" language=\"java\" %>");
        out.println(ServletUtilities.headWithTitle(title));
        out.println("<BODY>");
        out.println("<H1 align=\"center\">");
        out.println("Flight Booking Centre");
        out.println("</H1>");
        out.println("<br>");
        out.println("<p align=\"center\">Hi~ " + session.getAttribute("username") + "<a href=\"index.jsp?logout=1\">&nbspLOG OUT</a></p>");

        out.println("<br>");
        out.println("<FORM METHOD=POST ACTION=\"/Servlet\">");
        out.println("<TABLE align=\"center\">");
        out.println("<TR><TD>Select departing city</TD>");
        out.println("<TD><select name=\"deptcity\">");
        for (String city : citylist) out.println("<option value=\"" + city + "\">" + city + "</option>");
        out.println("</select></TD>");
        out.println("<TD>Select destination city</TD>");
        out.println("<TD><select name=\"destcity\">");
        for (String city : citylist) out.println("<option value=\"" + city + "\">" + city + "</option>");
        out.println("</select></TD></TR>");
        out.println("<TR><TD>Select departing date</TD>");
        out.println("<TD>2014-03-<select name=\"deptdate\">");
        for (int i = 0; i < 31; i++) {
            out.println("<option value=\"" + (i+1) + "\">" + (i+1) + "</option>");
        }
        out.println("</select></TD>");
        out.println("<TD>Select return date</TD>");
        out.println("<TD>2014-03-<select name=\"returndate\">");
        for (int i = 0; i < 31; i++) {
            out.println("<option value=\"" + (i+1) + "\">" + (i+1) + "</option>");
        }
        out.println("</select></TD></TR>");
        out.println("</TABLE>");
        out.println("<div style=\"text-align:center\">");
        out.println("<input type=\"submit\" value=\"QUERY\">");
        out.println("</div>");
        out.println("</FORM></BODY></HTML>");

    }

}
